package com.company.ljh.easy;

/**
 * @description: 1768. 交替合并字符串
 * 给你两个字符串 word1 和 word2 。请你从 word1 开始，通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长，就将多出来的字母追加到合并后字符串的末尾。
 *
 * 返回 合并后的字符串 。
 * @projectName:leet_code
 * @see:com.company.ljh.easy
 * @author:ljh
 * @createTime:2023/6/28 11:50
 * @version:1.0
 */
public class 交替合并字符串 {
    public static String mergeAlternately(String word1, String word2) {
        if(word2 == null || "".equals(word2)){
            return word1;
        }
        if(word1 == null || "".equals(word1)){
            return word2;
        }
        int index = 0;
        char [] newStr= new char[word1.length()+word2.length()];
        int indexWord1 = 0;
        int indexWord2 = 0;
        while(indexWord1 < word1.length() && indexWord2 < word2.length()){
            if(index % 2 == 0){
                newStr[index++] = word1.charAt(indexWord1++);
            }else{
                newStr[index++] = word2.charAt(indexWord2++);
            }
        }
        while(indexWord1 < word1.length()){
            newStr[index++] = word1.charAt(indexWord1++);
        }
        while(indexWord2 < word2.length()){
            newStr[index++] = word2.charAt(indexWord2++);
        }
        return String.valueOf(newStr);
    }
    public static void main(String[] args) {
        String str1 = "acw";
        String str2 = "fsx";
        String s = mergeAlternately(str1, str2);
        System.out.println(s);

    }
}
